<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/16
 * Time: 14:28
 */

namespace app\admin\controller;


use app\admin\model\AdminMenu;
use app\admin\model\AdminRole;
use app\admin\model\AdminUser;
use app\admin\model\WxSet;
use think\Db;

class Admin extends Base
{
    //管理员处理
    public function adminList(){
        return $this->fetch();
    }
    public function adminData($page,$limit){
        $data = AdminUser::page($page,$limit)->select()->each(function ($item){
            $item->role_id = AdminRole::where('id',$item->role_id)->value('role_name');
        })->toArray();
        $count = AdminUser::count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    public function adminAdd(){
        if (request()->isPost()){
            $username = trim(input('post.username'));
            $password = trim(input('post.password'));
            $r_pass = trim(input('post.r_pass'));
            $phone = trim(input('post.phone'));
            $role_id = input('post.role_id');
            $status = (int)input('post.status');
            $user = AdminUser::where('username',$username)->find();
            if (!preg_match("/^[a-zA-Z]\w{5,17}$/",$password)){
                return json(['code'=>0,'msg'=>'密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线']);
            }
            if ($password != $r_pass){
                return json(['code'=>0,'msg'=>'两次输入密码不一致']);
            }
            if (!$role_id){
                return json(['code'=>0,'msg'=>'请选择角色']);
            }
            if ($user){
                return json(['code'=>0,'msg'=>'用户名已存在']);
            }
            $data=[
                'username'=>$username,
                'password'=>md5(md5($username.md5($password)).'ADMIN'),
                'phone'=>$phone,
                'role_id'=>$role_id,
                'status'=>$status
            ];
            $ret = AdminUser::create($data);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        $role = AdminRole::where('status',1)->field('id,role_name')->select()->toArray();
        $this->assign('role',$role);
        return $this->fetch();
    }
    public function adminEdit($id){
        if (request()->isPost()){
            $username = trim(input('post.username'));
            $password = trim(input('post.password'));
            $r_pass = trim(input('post.r_pass'));
            $phone = trim(input('post.phone'));
            $role_id = input('post.role_id');
            $status = (int)input('post.status');
            $admin_id = input('post.id');
            if (!$role_id){
                return json(['code'=>0,'msg'=>'请选择角色']);
            }
            $data=[
                'phone'=>$phone,
                'role_id'=>$role_id,
                'status'=>$status
            ];
            if ($password){
                if (!preg_match("/^[a-zA-Z]\w{5,17}$/",$password)){
                    return json(['code'=>0,'msg'=>'密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线']);
                }
                if ($password != $r_pass){
                    return json(['code'=>0,'msg'=>'两次输入密码不一致']);
                }
                $data=[
                    'password'=>md5(md5($username.md5($password)).'ADMIN'),
                    'phone'=>$phone,
                    'role_id'=>$role_id,
                    'status'=>$status
                ];
            }
            $ret = AdminUser::update($data,['id'=>$admin_id]);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        $role = AdminRole::where('status',1)->field('id,role_name')->select()->toArray();
        $admin = AdminUser::where('id',$id)->find();
        $this->assign('role',$role);
        $this->assign('admin',$admin);
        return $this->fetch();
    }
    public function adminDel(){
        $id = input('post.id');
        $ret = AdminUser::destroy($id);
        if (!$ret){
            return json(['code'=>0,'msg'=>'删除失败']);
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }


    //角色处理
    public function roleList(){
        return $this->fetch();
    }
    public function roleData($page,$limit){
        $data = AdminRole::page($page,$limit)->select()->each(function ($item){
            $item->rule = implode(',',AdminMenu::where('id','in',explode(',',$item->rule))->column('menu_name'));
        })->toArray();
        $count = AdminRole::count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    public function roleAdd(){
        if (request()->isPost()){
            $data['role_name'] = trim(input('post.role_name'));
            $menus = input('post.menu/a');
            $status = (int)input('post.status');
            if(!$data['role_name']){
                return json(['code'=>0,'msg'=>'角色名称不能为空']);
            }
            $data['rule'] = implode(',',$menus);
            $data['status'] = $status;
            $ret = AdminRole::create($data);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        $data = AdminMenu::where('status',1)->field('id,menu_name,pid')->select()->toArray();
        $ret = $this->roleArray($data);
        $this->assign('data',$ret);
        return $this->fetch();
    }
    public function roleEdit($id){
        if (request()->isPost()){
            $data['role_name'] = trim(input('post.role_name'));
            $menus = input('post.menu/a');
            $status = (int)input('post.status');
            $role_id = (int)input('post.id');
            if(!$data['role_name']){
                return json(['code'=>0,'msg'=>'角色名称不能为空']);
            }
            $data['rule'] = implode(',',$menus);
            $data['status'] = $status;
            $ret = AdminRole::update($data,['id'=>$role_id]);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        $role = AdminRole::where('id',$id)->find();
        $role['rule'] = explode(',',$role['rule']);
        $data = AdminMenu::where('status',1)->field('id,menu_name,pid')->select()->toArray();
        $ret = $this->roleArray($data);
        $this->assign('role',$role);
        $this->assign('data',$ret);
        return $this->fetch();
    }
    public function roleDel(){
        $id = input('post.id');
        $ret = AdminRole::destroy($id);
        if (!$ret){
            return json(['code'=>0,'msg'=>'删除失败']);
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }


    //权限菜单处理
    public function menuList(){
        return $this->fetch();
    }
    public function menuData($page,$limit){
        $data = AdminMenu::order('sort asc')->page($page,$limit)->select()->each(function ($item){
            $item->pid = AdminMenu::where('id',$item->pid)->value('menu_name');
            $item->pid = $item->pid?$item->pid:'无';
        })->toArray();
        $count = AdminMenu::count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    public function menuAdd(){
        if (request()->isPost()){
            $menu_name = trim(input('post.menu_name'));
            $controller = trim(input('post.controller'));
            $action = trim(input('post.action'));
            $icon = trim(input('post.icon'));
            $sort = (int)input('post.sort');
            $pid = (int)input('post.pid');
            $type = (int)input('post.type');
            $status = (int)input('post.status');
            $data = [
                'menu_name'=>$menu_name,
                'controller'=>$controller,
                'action'=>$action,
                'sort'=>$sort,
                'icon'=>$icon,
                'pid'=>$pid,
                'type'=>$type,
                'status'=>$status,
            ];
            $ret = AdminMenu::create($data);
            if (!$ret){
                return json(['code'=>0,'msg'=>'添加失败']);
            }
            return json(['code'=>1,'msg'=>'添加成功']);
        }
        $icon = Db::name('admin_icon')->select();
        $one = AdminMenu::where('pid',0)->field('id,menu_name')->select()->toArray();
        $this->assign('icon',$icon);
        $this->assign('one',$one);
        return $this->fetch();
    }
    public function menuEdit($id){
        if (request()->isPost()){
            $menu_name = trim(input('post.menu_name'));
            $controller = trim(input('post.controller'));
            $action = trim(input('post.action'));
            $icon = trim(input('post.icon'));
            $sort = (int)input('post.sort');
            $pid = (int)input('post.pid');
            $type = (int)input('post.type');
            $status = (int)input('post.status');
            $menu_id = (int)input('post.id');
            $data = [
                'menu_name'=>$menu_name,
                'controller'=>$controller,
                'action'=>$action,
                'sort'=>$sort,
                'icon'=>$icon,
                'pid'=>$pid,
                'type'=>$type,
                'status'=>$status,
            ];
            $ret = AdminMenu::update($data,['id'=>$menu_id]);
            if (!$ret){
                return json(['code'=>0,'msg'=>'修改失败']);
            }
            return json(['code'=>1,'msg'=>'修改成功']);
        }
        $icon = Db::name('admin_icon')->select();
        $one = AdminMenu::where('pid',0)->field('id,menu_name')->select()->toArray();
        $info = AdminMenu::where('id',$id)->find();
        $this->assign('icon',$icon);
        $this->assign('one',$one);
        $this->assign('info',$info);
        return $this->fetch();
    }


    public function wxSet(){
        $data = WxSet::get(1);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function wxSetSave(){
        $post = input('post.');
        $data = [
            'appID'=>$post['appID'],
            'secret'=>$post['secret'],
            'mch_id'=>$post['mch_id'],
            'key'=>$post['key'],
        ];
        $ret = WxSet::update($data,['id'=>1]);
        if (!$ret){
            return json(['code'=>0,'msg'=>'保存失败']);
        }
        return json(['code'=>1,'msg'=>'保存成功']);
    }




}